package com.student.view;

import com.student.bean.Admin;
import com.student.dao.AdminDAO;
import com.student.dao.AdminDAO;
import com.student.utils.Tools;
import javafx.scene.text.FontSmoothingType;

import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class loginview {
    private JFrame frame;
    private final int WIDTH = 500;
    private final int HEIGHT = 500;
    public static String pow = "0";//用户

    public loginview() {

        initView();
        frame.setTitle("高校学籍管理系统");
        frame.setLayout(null);
        frame.setVisible(true);
        frame.setResizable(false);
        Tools.setPos(frame, WIDTH, HEIGHT);
        frame.validate();//让组件生效
    }

    /**
     * 初始化窗口
     */
    private void initView() {
        if (frame == null) {
            frame = new JFrame();
        }
        JPanel jPanel = new JPanel();
        jPanel.setBounds(0, 0, WIDTH, HEIGHT);
        jPanel.setLayout(null);
        frame.add(jPanel);
//创建标签，用以存储图片，达到背景图片目的
        ImageIcon icon = new ImageIcon("image/2.jpeg");
        JLabel lable = new JLabel(icon);
        lable.setBounds(0, 0, WIDTH, HEIGHT);

//标题
        JLabel title = new JLabel("高校学籍管理系统");
        title.setBounds(WIDTH / 2 - 150, 10, 300, 40); // 调整位置和大小
        title.setFont(new Font("宋体", Font.BOLD, 28));
        title.setForeground(Color.blue);
        jPanel.add(title);

        // 添加账号输入框
        JLabel usernameLabel = new JLabel("账号:");
        usernameLabel.setBounds(50, 70, 100, 30);
        jPanel.add(usernameLabel);

        JTextField usernameField = new JTextField();
        usernameField.setBounds(150, 70, 200, 30);
        jPanel.add(usernameField);

        // 添加密码输入框
        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setBounds(50, 110, 100, 30);
        jPanel.add(passwordLabel);

        JPasswordField passwordField = new JPasswordField();
        passwordField.setBounds(150, 110, 200, 30);
        jPanel.add(passwordField);
        frame.add(jPanel);
        jPanel.add(lable);

        // 创建登录按钮
        JPanel jPanel222 = new JPanel();
        jPanel222.setBounds(0, 180, WIDTH, 30);
        jPanel222.setLayout(new FlowLayout(FlowLayout.CENTER));
        jPanel.add(jPanel222);
        jPanel222.setOpaque(false);

        JButton jButton = new JButton("登录");
        jPanel222.add(jButton);

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取账号和密码
                String account = usernameField.getText();
                String password = new String(passwordField.getPassword());

                // 检查账号和密码是否为空
                if (account.isEmpty() || password.isEmpty()) {
                    JOptionPane.showMessageDialog(frame, "账号和密码不能为空", "警告", JOptionPane.WARNING_MESSAGE);
                } else {
                    Admin admin = AdminDAO.isLogin(account, password);
                    if (admin==null){
                        System.out.println("账号密码错误");
                    }else{
                        // 显示登录成功对话框，等待用户点击确认
                        int result = JOptionPane.showConfirmDialog(frame, "登录成功", "成功", JOptionPane.OK_CANCEL_OPTION);

                        // 如果用户点击了确认按钮
                        if (result == JOptionPane.OK_OPTION) {
                            // 显示ManageView界面
                            ManageView manageView = new ManageView();
                            manageView.setVisible(true);

                            // 关闭当前窗口
                            frame.dispose();
                        }
                    }
                }
            }
        });








        /* *
         *JLabel register=new JLabel("注册账户");
         *  jPanel.add(register);
         *  register.setBounds(10,110,100,40);
         *  register.setFont(new Font("宋体",Font.PLAIN,11));
         *  register.setForeground(new Color(166,166,166));
         */
    }

}
